class Solution{
public:
  int *last = nullptr;

  bool isValidBST(TreeNode *root){

    if(root){
      if(!isValidBST(root->left)){
        return false;
      }
      if(last && *last >= root->val){
        return false;
      }
      last = &root->val;
      if(!isValidBST(root->right)){
        return false;
      }
      return true;
    }

    return true;

  };
};